<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Reservation_model extends MY_Model {
	
	public function __construct(){
		parent::__construct();	
		$this->setTableName('u_m_reservation');
	}
	
	public function reserve($uid,$sid,$type,$time=0){
		$array = [];
		$array['user'] = $uid;
		$array['sid'] = $sid;
		$array['status'] = 1;
		$array['worker'] = 1;
		date_default_timezone_set("Asia/Shanghai");
		$t = time();
		$array['create_time'] = $t; 
		$array['update_time'] = $t;
		$array['type'] = $type;
		$array['time'] = $time==0?date("Y-m-d H:i:s"):$time;
		$re = $this->insert($array);
		return $re;
	}
	
	public function reserveByUser($array){
		$array['sid'] = 0;
		$array['status'] = 1;
		$t = time();
		$array['create_time'] = $t; 
		$array['update_time'] = $t;
		$re = $this->insert($array);
		return $re;
	}
	
	public function reservationList($uid){
		$what = "wq_u_m_server.title,wq_u_c_status.name as status,wq_u_c_worker.name as wname,wq_u_c_user.username as uname,wq_u_c_user.tel as utel";
		$table = "wq_u_m_reservation left join wq_u_m_server on wq_u_m_reservation.sid=wq_u_m_server.id,wq_u_c_status,wq_u_c_worker,wq_u_c_user";
		$where = "wq_u_m_reservation.user={$uid} AND wq_u_m_reservation.status=wq_u_c_status.classid AND wq_u_m_reservation.worker=wq_u_c_worker.classid AND wq_u_c_user.classid=wq_u_m_reservation.user;";
		$sql = "SELECT {$what} FROM {$table} WHERE {$where}";
		$re = $this->db->query($sql)->result_array();
		return $re;
	}
	
	public function reservationListByWorker($worker){
		$what = "wq_u_m_server.title,wq_u_c_status.name as status,wq_u_c_worker.name as wname,wq_u_c_user.username as uname,wq_u_c_user.tel as utel,wq_u_c_type.name as type,wq_u_c_type.weight*wq_u_m_server.price*wq_u_m_server.discount*0.1 as price,wq_u_c_user.plate as plate";
		$table = "wq_u_m_reservation left join wq_u_m_server on wq_u_m_reservation.sid=wq_u_m_server.id,wq_u_c_status,wq_u_c_worker,wq_u_c_user,wq_u_c_type";
		$where = "wq_u_c_worker.classid={$worker} AND wq_u_m_reservation.status=wq_u_c_status.classid AND wq_u_m_reservation.worker=wq_u_c_worker.classid AND wq_u_c_user.classid=wq_u_m_reservation.user AND wq_u_m_reservation.type=wq_u_c_type.classid";
		$sql = "SELECT {$what} FROM {$table} WHERE {$where}";
		$re = $this->db->query($sql)->result_array();
		return $re;
	}
}
?>